package com.linghang.wusthelper.exceptionhandler;


import com.alibaba.fastjson.JSONObject;
import com.linghang.wusthelper.exception.AuthenException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author origin
 * 异常处理类
 */
@ControllerAdvice
public class ControllerExceptionHandler {


    private Logger log = LoggerFactory.getLogger(getClass());

    /**
     * 认证异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(AuthenException.class)
    public Object handleAuthenException(AuthenException e){
        String msg = e.getMessage();
        if (msg == null || msg.equals("")){
            msg = "服务器异常";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message",msg);
        if (msg.equals("签名无效或已过期")){
            jsonObject.put("status","-1");
        }else {
            log.warn("检测到非法请求");
        }
        return jsonObject;
    }

    @ResponseBody
    @ExceptionHandler(InterruptedException.class)
    public Object handleInterruptedException(InterruptedException e){
        String msg = e.getMessage();
        if (msg == null || msg.equals("")){
            msg = "服务器异常";
        }
        log.error("爬虫线程中断异常，请及时处理");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",1);
        jsonObject.put("msg","后台异常，请与维护人员联系");
        return jsonObject;
    }
}
